/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package myservlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import myservlet.OnlineCounter;
import org.apache.log4j.Logger;

public class OnlineCounterListener implements HttpSessionListener, ServletContextListener {
	private int count = 0;
	private ServletContext servletContext = null;
	private Logger logger = Logger.getLogger(this.getClass());

	public void sessionCreated(HttpSessionEvent event) {
		OnlineCounter.raise();
		++this.count;
		this.setContext(event);
		this.logger.info("***************the  http session is created...***************");
	}

	public void sessionDestroyed(HttpSessionEvent event) {
		OnlineCounter.reduce();
		--this.count;
		this.setContext(event);
		this.logger.info("***************the  http session is destroyed...***************");
	}

	public void contextDestroyed(ServletContextEvent event) {
		this.servletContext = null;
		this.logger.info("***************the  servlet context is destroyed...***************");
	}

	public void contextInitialized(ServletContextEvent event) {
		this.servletContext = event.getServletContext();
		this.logger.info("***************the  servlet context is initialized...***************");
	}

	public void setContext(HttpSessionEvent httpSessionEvent) {
		httpSessionEvent.getSession().getServletContext().setAttribute("online", Integer.valueOf(this.count));
	}
}